<?php
/**
 * CJuiDateTimePicker class file.
 *
 * @author Anatoly Ivanchin <van4in@gmail.com>
 */

Yii::import('ext.CJuiDateTimePicker.CJuiDateTimePicker');
class CJuiDateTimePickerPlus extends CJuiDateTimePicker
{
    const ASSETS_NAME = '/jquery-ui-timepicker-addon';
    
    public $assetsPath = '';
    public $format;
    
    public function init(){
        parent::init();
        $this->assetsPath = Yii::getPathOfAlias('ext.CJuiDateTimePicker').'/';
    }
    
	public function run()
	{
		list($name,$id)=$this->resolveNameID();

		if(isset($this->htmlOptions['id']))
			$id=$this->htmlOptions['id'];
		else
			$this->htmlOptions['id']=$id;
		if(isset($this->htmlOptions['name']))
			$name=$this->htmlOptions['name'];
		else
			$this->htmlOptions['name']=$name;

		if ($this->format === null){
		    if($this->hasModel())
		        echo CHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);
		    else
		        echo CHtml::textField($name,$this->value,$this->htmlOptions);
		}else{
		    if($this->hasModel())
		        echo CHtml::activeTextField($this->model,call_user_func($this->format,$this->attribute),$this->htmlOptions);
		    else
		        echo CHtml::textField($name,call_user_func($this->format,$this->value),$this->htmlOptions);
		}
		
		


		$options=CJavaScript::encode($this->options);

		$js = "jQuery('#{$id}').{$this->mode}picker($options);";

		if (isset($this->language)){
			$this->registerScriptFile($this->i18nScriptFile);
			$js = "jQuery('#{$id}').{$this->mode}picker(jQuery.extend({showMonthAfterYear:false}, jQuery.datepicker.regional['{$this->language}'], {$options}));";
		}

		$cs = Yii::app()->getClientScript();
		
		$assets = Yii::app()->getAssetManager()->publish( $this->assetsPath.'assets');
		$cs->registerCssFile($assets.self::ASSETS_NAME.'.css');
		$cs->registerScriptFile($assets.self::ASSETS_NAME.'.js',CClientScript::POS_END);
		
		$cs->registerScript(__CLASS__, 	$this->defaultOptions?'jQuery.{$this->mode}picker.setDefaults('.CJavaScript::encode($this->defaultOptions).');':'');
		$cs->registerScript(__CLASS__.'#'.$id, $js);

	}
}